import Vue from 'vue';
import axios from 'axios'
import qs from 'qs'

//超时时间
axios.defaults.timeout = 5000;
//默认头部类型
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8';

axios.interceptors.request.use((config) => {
    Vue.$vux.loading.show({text: 'Loading'});
	if(config.method  === 'post'){
	    config.data = qs.parse(config.data);
	}
  	return config;
},(error) =>{
  	Vue.$vux.toast.show({type : 'warn', text: '请求发生异常,请重试'});return false;
});

axios.interceptors.response.use((res) =>{
    Vue.$vux.loading.hide();
    var data = res.data;
    if(data['code']==-10000){
        window.location.href="/auth/#/login?path="+encodeURI(data['path']);return false;
    }
    if(data['code']!=0){
        Vue.$vux.toast.show({type : 'warn', text: data['msg']});
        return false;
    }else if(data['code']==0 && data['msg']!=''){
        Vue.$vux.toast.show({type : 'success', text: data['msg']});
    }
    return res;
}, (error) => {
    Vue.$vux.toast.show({type : 'warn', text: '获取发生异常,请重试'});return false;
});


export function fetch(url, params,type='post') {
    return new Promise((resolve, reject) => {
    	var ax = axios;
    	switch(type){
    		case "put":
    			ax = ax.put(url, params);
    		break;
    		case "delete":
    			ax = ax.delete(url,{params:params});
    		break;
    		case "post":
    			ax = ax.post(url, params);
    		break;
    		case "get":
    			ax = ax.get(url, {params : params});
    		break;
    	}
    	ax.then(response => {
            resolve(response.data);
        }, err => {
            reject(err);
        })
        .catch((error) => {
           reject(error)
        })
    })
}

//模块前缀
var pre = {
    Home : 'home/',
    Auth : 'auth/'
}

export default {
    //发送手机验证码
    SendPhoneCode(url,params){
        return fetch(url,params);
    },
	//个人中心
	Home:{
        //基本信息
		BaseInfo(params) {
            return fetch(pre.Home+'baseinfo',params,'get');
        },
        //获取所有个人信息
        Info(params){
            return fetch(pre.Home+'info',params,'get');  
        },
        //保存个人信息
        InfoSave(params){
            return fetch(pre.Home+'infosave',params);  
        },
        //绑定手机号
        BindPhone(params){
            return fetch(pre.Home + 'account/bindphone',params);
        },
        //更新账户密码
        UpdatePwd(params){
            return fetch(pre.Home + 'account/updatepwd',params);   
        },
        //地址管理
        Address :{
            //获取
            Get(id){
                return fetch(pre.Home + 'address/'+id,{},'get');
            },
            //获取列表
            GetList(params){
                return fetch(pre.Home + 'address',params,'get');
            },
            //更新
            Update(params){
                return fetch(pre.Home + 'address',params);
            },
            //更新
            Delete(params){
                return fetch(pre.Home + 'address/',params,'delete');
            },
            
        }
	},
    Auth : {
        Login(params) {
            return fetch('login',params);
        },
        Logout(){
            return fetch(pre.Auth + 'logout',{});  
        },
        Register : {
            //手机号码验证码
            PhoneCheck(params){
                return fetch('register/phone/check',params);
            },
            //手机验证码验证码
            CodeCheck(params){
                return fetch('register/phone/code/check',params);
            },
            //注册
            Do(params){
                return fetch('register/phone',params);
            }
        },
        Password :{
            //手机号码验证码
            PhoneCheck(params){
                return fetch('password/phone/check',params);
            },
            //手机验证码验证码
            CodeCheck(params){
                return fetch('password/phone/code/check',params);
            },
            //注册
            Do(params){
                return fetch('password/phone',params);
            }
        }
    }

}